package com.smartcruise.main.selfFragment.setting.VerifyPasswordFragment;

import android.text.Editable;
import android.text.TextWatcher;
import android.widget.Button;

import com.smartcruise.R;
import com.smartcruise.base.BaseTitleFragment;
import com.smartcruise.base.TitleBuilder;
import com.smartcruise.start.modifyPassword.ModifyPasswordFragment;
import com.smartcruise.util.FragmentUtils;
import com.smartcruise.util.customView.ClearableEditTextWithIcon;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by FengChaoQun
 * on 2018/3/17 0017
 */

public class VerifyPasswordFragment extends BaseTitleFragment implements VerifyPasswordContract.View {
    @BindView(R.id.password)
    ClearableEditTextWithIcon password;
    @BindView(R.id.submit)
    Button submit;

    private VerifyPasswordContract.Presenter mPresenter;

    @Override
    protected void init() {
        mPresenter = new VerifyPasswordPresenterImp(this);
        mPresenter.start();

        password.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (password.length() >= 6) {
                    submit.setAlpha(1f);
                } else {
                    submit.setAlpha(0.5f);
                }
            }
        });
    }

    @Override
    protected int getContentLayout() {
        return R.layout.fragment_contain_verift_password;
    }

    @Override
    protected TitleBuilder getTitleBuilder() {
        return TitleBuilder.create(true, getString(R.string.verifyPassword));
    }

    @OnClick(R.id.submit)
    public void onViewClicked() {
        if (password.length() >= 6) {
            mPresenter.clickOnSubmit();
        } else {
            showToast("密码不得少于6位");
        }
    }

    @Override
    public String getPassword() {
        return password.getText().toString();
    }

    @Override
    public void gotoSetPassword() {
        FragmentUtils.replace(getFragmentManager(), R.id.container, ModifyPasswordFragment.getInstance(false, null));
    }
}
